package com.ftg.shopping.user.service;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ftg.shopping.config.LayuiPage;

import com.ftg.shopping.user.entity.sp_userinfo;
import com.ftg.shopping.user.mapper.UserMapper;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserService {

    @Autowired
    UserMapper userMapper;

    public List<sp_userinfo> list(){
        QueryWrapper qw  = new QueryWrapper();
        Map<String,Object> map = new HashMap<>();
        map.put("userid" , 0);
        map.put("username" , null);
        map.put("password" , null);
        map.put("email" , null);
        qw.allEq(false,(k , v) -> !k.equals("username") , map , false);
        return userMapper.selectList(null);
    }

    public sp_userinfo findone(int userid) {
        return userMapper.selectById(userid);
    }

    public int saveUser(sp_userinfo u){

        return userMapper.insert(u);
    }

    public LayuiPage<sp_userinfo> login(sp_userinfo s) {

        IPage<sp_userinfo> ip = new Page<>();
        LayuiPage user = new LayuiPage<>(ip);
        QueryWrapper qw = new QueryWrapper();
        Map<String, Object> map = new HashMap<>();
        map.put("username", s.username);
        map.put("password", s.password);
        qw.allEq(map, false);
        if (userMapper.selectList(qw).isEmpty()) {
            user.setStatus("failure");
            return user;
        } else {
            user.setStatus("success");
            user.setData(userMapper.selectList(qw));
            return user;
        }

    }


    public int updateDept(sp_userinfo u){
        UpdateWrapper qw = new UpdateWrapper();
        qw.set("username",u.username);
        qw.set("password",u.password);
        qw.set("email",u.email);
        qw.eq("userid",u.userid);
        return userMapper.update(u,qw);
    }

    public int delDept(int userid){
        return userMapper.deleteById(userid);
    }

    public int delDeptIds(List<Integer> ids){
        return userMapper.deleteBatchIds(ids);
    }

    public IPage<sp_userinfo> testPage(int page, int limit){
        IPage<sp_userinfo> ip = new Page<>(page,limit);
        return userMapper.selectPage(ip,null);
    }


    public IPage<sp_userinfo> testPage1(int page, int limit, String username){
        IPage<sp_userinfo> ip = new Page<>(page,limit);
        QueryWrapper qw = new QueryWrapper();
        qw.like("username",username);
        return userMapper.selectPage(ip,username ==null || username.equals("") ? null:qw);
    }

    public LayuiPage<sp_userinfo> testPage2(int page, int limit){

        IPage<sp_userinfo> ip = new Page<>(page,limit);

        userMapper.selectPage(ip,null);

        return new LayuiPage<>(ip) ;
    }
}
